#!/usr/bin/python  
#-*-coding:utf-8-*- 

'''
主要执行下载操作

1、查询所有的下载存储帐号池中的 帐号



'''
import requests
import json
import sys
import sqlite3
import random
import time
import re
reload(sys)
sys.setdefaultencoding('utf-8')


headers ={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'}

headers_bmob ={'X-Bmob-Application-Id':'93adb30d37cf715d8e7961a10480d517','X-Bmob-REST-API-Key':'5d0715ba1dbd71325e9d993a7eb5d8c1','Content-Type':'application/json'}

phones_all=[]

def query_all_csdn_for_download():
	'''查询所有的下载存储帐号池中的帐号
	'''

	params ={"limit":"1000","skip":"0",'keys':'objectid,username,password,email,email_password,status','order':'createdAt'}
	resp=requests.get('https://api.bmob.cn/1/classes/accounts_store?where={"status":3}',headers=headers_bmob,params=params)
	j =json.loads(resp.content)
	accounts = list(j['results'])

def query_phones(step):
	print 'skip :%s' % str(step)
	params = {"limit": "1000","skip":str(step)}
	try:
		resp = requests.get('https://api.bmob.cn/1/classes/MobileInfo', params=params, headers=headers_bmob, timeout=15)
	except Exception, e:
		raise
	else:
		if resp.status_code != 200:
			raise ValueError('query all count error ')
		else:
			j = json.loads(resp.content)
			phones_all.extend(j['results'])

def getValueByKey(info,key):
	if info.has_key(key):
		return info[key]
	else:
		return "none"

def insert_a_phones(phone_info,cursor):
	'''
	插入一条记录到数据库
	:param phone_info:
	:return:
	'''
	#no such

	cursor.execute('insert into phones '
				   '('
					   'model,manufacturer,hardware,brand,board,'
				   	   'release,product,bootloader,cpu_abi,cpu_abi2,'
				   	   'device,display,fingerprint,incermental,radio,'
				   		'sdk_int,dpi,width,height'
				   ') '
				   'values '
				   '('
					   '\'%s\',\'%s\',\'%s\',\'%s\',\'%s\','
				   	   '\'%s\',\'%s\',\'%s\',\'%s\',\'%s\','
					   '\'%s\',\'%s\',\'%s\',\'%s\',\'%s\','
					   '\'%s\',\'%s\',\'%s\',\'%s\''
				   ')'
				   %
				   (
					   getValueByKey(phone_info,'MODEL'),getValueByKey(phone_info,'MANUFACTURER'),getValueByKey(phone_info,'HARDWARE'),getValueByKey(phone_info,'BRAND'),getValueByKey(phone_info,'BOARD'),
					   getValueByKey(phone_info,'RELEASE'),getValueByKey(phone_info,'PRODUCT'),getValueByKey(phone_info,'BOOTLOADER'),getValueByKey(phone_info,'CPU_ABI'),getValueByKey(phone_info,'CPU_ABI2'),
					   getValueByKey(phone_info,'DEVICE'),getValueByKey(phone_info,'DISPLAY'),getValueByKey(phone_info,'FINGERPRINT'),getValueByKey(phone_info,'INCREMENTAL'),getValueByKey(phone_info,'RADIO'),
					   getValueByKey(phone_info,'HSDK_INT'),getValueByKey(phone_info,'DPI'), getValueByKey(phone_info,'WIDTH') ,getValueByKey(phone_info,'HEIGHT')
					)
				   )







def query_all_in_bmob():
	'''
	查询所有采集到的设备信息
	:return:
	'''
	##1 获取 个数
	##2 每次1000计算 需要的次数
	##3 执行请求 注意skip

	params = {"limit": "0", "count": "1"}
	all_count =0
	try:
		resp=requests.get('https://api.bmob.cn/1/classes/MobileInfo',params=params,headers=headers_bmob,timeout=15)
	except Exception, e:
		raise
	else:
		if resp.status_code!=200:
			raise ValueError('query all count error ')
		else:
			j = json.loads(resp.content)
			all_count=int(j['count'])

	if all_count==None or all_count==0:
		raise ValueError('count is zero')

	step =all_count/1000
	if(all_count % 1000 !=0):
		step=step+1
	for a in range(step):
		query_phones(a)

	#过滤
	mp1 ={}
	global phones_all
	map(lambda x:mp1.setdefault(x['MODEL'],x),phones_all)
	print type(mp1.values())
	phones_all =list(mp1.values())
	print phones_all

	#开始 构建sqlite
	conn = sqlite3.connect('phones.db')
	cursor = conn.cursor()
	for phone_info in phones_all:
		insert_a_phones(phone_info,cursor)

	cursor.close()
	conn.commit()
	conn.close()


def	upload_to_bmob():
	'''
	将本地数据库上传到bmob
	:return:
	'''
	conn = sqlite3.connect('phones.db')
	cursor = conn.cursor()
	cursor.execute("select * from phones")
	values = cursor.fetchall()
	l1=[]
	for info in values:
		print str(info)
		map1 ={}
		map1['model']=str(info[1])
		map1['manufacturer']=str(info[2])
		map1['hardware']=str(info[3])
		map1['brand']=str(info[4])
		map1['board']=str(info[5])
		map1['release']=str(info[6])
		map1['product']=str(info[7])
		map1['bootloader']=str(info[8])
		map1['cpu_abi']=str(info[9])
		map1['cpu_abi2']=str(info[10])
		map1['device']=str(info[11])
		map1['display']=str(info[12])
		map1['fingerprint']=str(info[13])
		map1['incermental']=str(info[14])
		map1['radio']=str(info[15])
		map1['sdk_int']=str(info[16])
		map1['dpi']=str(info[17])
		map1['width']=str(info[18])
		map1['height']=str(info[19])
		l1.append(map1)

	print str(len(l1))


	#上传到bmob

	def spilt_array(news_array):
		print 'a'
		list1=[]
		for info in news_array:
			map1={'method':'POST','path':'/1/classes/PhoneBuildInfo','body':info}
			list1.append(map1)
		map2={'requests':list1}
		json1=json.dumps(map2)
		resp=requests.post('https://api.bmob.cn/1/batch',data=json1,headers=headers_bmob,timeout=10)
		print resp.content
	stepnum=len(l1)/45
	if not len(l1)%45==0:
		stepnum+=1
	for index in range(stepnum):
		spilt_array(l1[index*45:(index+1)*45])
















if __name__=='__main__':
	upload_to_bmob()




